<?php

namespace App\Kuafu\CustomerCoupon\Controller\B;

use App\Kuafu\CustomerCoupon\Validate\CustomerCouponValidate;
use App\Kuafu\CustomerCoupon\Enum\CustomerCouponCodeEnum;
use App\Kuafu\CustomerCoupon\Service\CustomerCouponService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class CustomerCouponController extends BaseController
{
    /**
     * 根据id获取详情
     * @param $request
     * @param $configId
     */
    public function getCustomerCouponById(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = CustomerCouponValidate::getCustomerCouponById(['id' => $id]);
        if ($valid) {
            return $this->fail(CustomerCouponCodeEnum::ERR_CUSTOMERCOUPON_PARAM_VALID, $valid);
        }
        $info = CustomerCouponService::getCustomerCouponById($id);

        return $this->success($info);
    }

    /**
     * 根据优惠券ID获取优惠券发放列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getCustomerCouponListById(Request $request, $couponId)
    {
        $phone                = (int)$request->get('phone', '');          //会员手机号
        $status               = (string)$request->get('status', '');
        $endTime              = (string)$request->get('endTime', '');     //领取优惠券开始时间
        $startTime            = (string)$request->get('startTime', '');   //领取优惠券结束时间
        $couponCode           = (int)$request->get('couponCode', '');     //优惠券码
        $validate['pageSize'] = $request->get('pageSize', 20);
        $validate['pageNum']  = $request->get('pageNum', 1);
        $valid                = CustomerCouponValidate::getCustomerCouponList($validate);
        if ($valid) {
            return $this->fail(CustomerCouponCodeEnum::ERR_CUSTOMERCOUPON_PARAM_VALID, $valid);
        }

        $lists = CustomerCouponService::getCustomerCouponList($couponId, $phone, $status, $couponCode, $startTime,
            $endTime, $validate['pageSize'], $validate['pageNum']);

        return $this->success(['lists' => $lists]);
    }

    /**
     * 新增
     * @param $request
     * @return JsonResponse
     */
    public function addCustomerCoupon(Request $request)
    {
        $valid = CustomerCouponValidate::addCustomerCoupon($request->all());
        if ($valid) {
            return $this->fail(CustomerCouponCodeEnum::ERR_CUSTOMERCOUPON_PARAM_VALID, $valid);
        }

        CustomerCouponService::addCustomerCoupon($request->all());
        return $this->success();
    }

    /**
     * 删除（软删除）
     * @param $request
     * @param $configId
     */
    public function delCustomerCoupon(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = CustomerCouponValidate::delCustomerCoupon(['id' => $id]);
        if ($valid) {
            return $this->fail(CustomerCouponCodeEnum::ERR_CUSTOMERCOUPON_PARAM_VALID, $valid);
        }

        CustomerCouponService::delCustomerCoupon($id);
        return $this->success();
    }

    /**
     * 根据id修改信息
     * @param $request
     */
    public function setCustomerCoupon(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = CustomerCouponValidate::setCustomerCoupon($request->all() + ['id' => $id]);
        if ($valid) {
            return $this->fail(CustomerCouponCodeEnum::ERR_CUSTOMERCOUPON_PARAM_VALID, $valid);
        }

        CustomerCouponService::setCustomerCoupon($id, $request->all());
        return $this->success();
    }

    /**
     * 上下架
     * @param $request
     */
    public function pathCustomerCouponStatus(Request $request, $id)
    {
        $status = $request->get('status');

        $id    = (int)$id;
        $valid = CustomerCouponValidate::patchCustomerCouponStatus($request->all() + ['id' => $id]);
        if ($valid) {
            return $this->fail(CustomerCouponCodeEnum::ERR_CUSTOMERCOUPON_PARAM_VALID, $valid);
        }

        CustomerCouponService::patchCustomerCouponStatus($id, $status);
        return $this->success();
    }


    /**
     * 优惠券数据列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function lists(Request $request): JsonResponse
    {
        $page             = (int)$request->get("pageNum", 1);
        $pageSize         = (int)$request->get("pageSize", 10);
        $suitShopId       = (int)$request->get("suitShopId", 0);
        $activityId       = (int)$request->get("activityId", 0);
        $activityType     = (int)$request->get("activityType", 0);
        $activityName     = (string)$request->get("activityName", "");
        $couponId         = (int)$request->get("couponId", 0);
        $couponType       = (int)$request->get("couponType", 0);
        $couponName       = (string)$request->get("couponName", "");
        $couponStatus     = $request->get("couponStatus");
        $username         = (string)$request->get("username", "");
        $uid              = (int)$request->get("uid", 0);
        $mobile           = (string)$request->get("mobile");
        $receiveTimeStart = (string)$request->get("receiveTimeStart", "");
        $receiveTimeEnd   = (string)$request->get("receiveTimeEnd", "");
        $useTimeStart     = (string)$request->get("useTimeStart", "");
        $useTimeEnd       = (string)$request->get("useTimeEnd", "");

        return $this->success(CustomerCouponService::lists(
            $suitShopId,
            $activityId,
            $activityType,
            $activityName,
            $couponId,
            $couponType,
            $couponName,
            $couponStatus,
            $username,
            $uid,
            $mobile,
            $receiveTimeStart,
            $receiveTimeEnd,
            $useTimeStart,
            $useTimeEnd,
            $page,
            $pageSize
        ));
    }
}
